package priv.pront.code.national.acwing.dp.sm;

import java.util.Scanner;

public class Acwing1057_股票买卖IV {
    static int N = 100005, M = 105, INF = 0x3f3f3f3f;
    static int[] w = new int[N];
    static int[][][] dp = new int[N][M][2];
    static int n, k;
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt(); k = scanner.nextInt();
        for(int i = 0; i <= n; i++){
            for(int j = 0; j <= k; j++){
                for(int k = 0; k < 2; k++){
                    dp[i][j][k] = -INF;
                }
            }
        }
        for(int i = 1; i <= n; i++) w[i] = scanner.nextInt();
        for(int i = 0; i <= n; i++){dp[i][0][0] = 0;}
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= k; j++){
                dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + w[i]);
                dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - w[i]);
            }
        }
        int ans = 0;
        for(int i = 1; i <= k; i++){
            ans = Math.max(ans, dp[n][i][0]);
        }
        System.out.println(ans);
    }
}
